<template>
  <div>
    <!-- 图文聊天有结束时间，查看是否过期 -->
	  <div v-if="allow">
      <span class="fl">问诊中</span><span class="fr">{{timeTxt}}后问诊结束</span>
	  </div>
	  <div v-else>
      <span class="fr">问诊已结束</span>
	  </div>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        timeTxt: '',
        allow: true, // 是否结束
        timer: null
      }
    },
    props: {
      time: '',
      endStatus: Boolean || false
    },
    mounted () {
      this.countdowm(this.time)
    },
    methods: {
		countdowm (num) {
			let that = this
      that.timer = setInterval(function () {
			  let nowTime = new Date();
			  let endTime = new Date(num * 1000);
			  let t = endTime.getTime() - nowTime.getTime();
			  if (t > 0) {
				let day = Math.floor(t / 86400000);
				let hour = Math.floor((t / 3600000) % 24);
				let min = Math.floor((t / 60000) % 60);
				let sec = Math.floor((t / 1000) % 60);
				hour = hour < 10 ? "0" + hour : hour;
				min = min < 10 ? "0" + min : min;
				sec = sec < 10 ? "0" + sec : sec;
				let format = '';
				if (day > 0) {
				  format = `${day}天${hour}时${min}分${sec}秒`;
				}
				if (day <= 0 && hour > 0) {
				  format = `${hour}时${min}分${sec}秒`;
				}
				if (day <= 0 && hour <= 0) {
				  format = `${min}分${sec}秒`;
				}
				that.timeTxt = format
			  } else {
          clearInterval(that.timer)
          that.allow = false
          that.$emit('chatEnd', true)
			  }
			}, 1000)
		  }
    },
    beforeDestroy () {
      clearInterval(this.timer)
    }
  }
</script>

<style>
</style>
